<?php


namespace app\common\cachemodel;


use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class ZlAstroCalendarGoods extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_astro_calendar_goods';
    protected $cacheModel = 'astro.calendar_goods';

    /**
     * 获取日期的
     * @param int $date
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function getGoodsToDate(int $date)
    {
        $goods = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $date)
            ->getDataAndSet(function () use ($date)
            {
                try
                {
                    $d = date('Y-m-d', strtotime($date));
                    $goods = $this->where('status', 1)
                        ->where('end_time', '> time', $d)
                        ->where('start_time', '<= time', $d)
                        ->field('id,title,img,img_scale,target,link')->select();

                    if ($goods)
                    {
                        $goods = collection($goods)->toArray();
                    }
                }
                catch (\Exception $e)
                {
                    $goods = [];
                }

                return json_encode($goods);
            });

        if (is_string($goods))
        {
            $goods = json_decode($goods, true);
        }

        return $goods;
    }
}
